﻿//-----------------------------------------------------------------------
// <copyright file="TaskBase.cs" company="Numbrella foundation">
// Copyright (c) Numbrella foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Numbrella.Core
{
	using System;
	using Numbrella.Interaction;
	
	/// <summary>
	/// Represents base class for all <see cref="ITask"/> implementations.
	/// </summary>
	/// <typeparam name="TResultContents">Type parameter for the associated <see cref="TaskResult{TContents}"/> implementation.</typeparam>
	[Serializable]
	public abstract class TaskBase<TResultContents> : ITask
	{
		#region Fields

		/// <summary>
		/// Contains unique task identifier.
		/// </summary>
		private readonly int id;

		#endregion

		#region Initialization

		/// <summary>
		/// Initializes a new instance of the <see cref="TaskBase{TResultContents}"/> class.
		/// </summary>
		/// <param name="id">Unique task identifier.</param>
		protected TaskBase(int id)
		{
			this.id = id;
		}

		#endregion

		#region Properties

		/// <summary>
		/// Gets unique task identifier.
		/// </summary>
		public int Id
		{
			get { return this.id; }
		}

		#endregion

		#region Methods

		/// <summary>
		/// Solves task and prepares <see cref="TaskResult{ResultContents}"/> instance containing task solve results.
		/// It also calculates amount of work that was done to solve the task actually.
		/// </summary>
		/// <returns>Results for solved task.</returns>
		public ITaskResult Perform()
		{
			TResultContents resultContents = this.ReallyPerform();
			return new TaskResult<TResultContents>(resultContents, this.id);
		}

		/// <summary>
		/// Implement this method to perform real task solving there.
		/// </summary>
		/// <returns>Result contents for a solved task.</returns>
		protected abstract TResultContents ReallyPerform();

		#endregion
	}
}
